﻿Type.registerNamespace('MS.InformationManagement.CostReport');
MS.InformationManagement.CostReport.StartDateControl = null;
MS.InformationManagement.CostReport.EndDateControl = null;
MS.InformationManagement.CostReport.PlottingElementID = null;
MS.InformationManagement.CostReport.TabContainerID = null;
MS.InformationManagement.CostReport.Plotted = false;

MS.InformationManagement.CostReport.Initialize = function (plottingElementID, startDateID, endDateID, triggerID, tabContainerID) {
    MS.InformationManagement.CostReport.PlottingElementID = plottingElementID;
    MS.InformationManagement.CostReport.TabContainerID = tabContainerID;
    MS.InformationManagement.Services.ReportService.set_path('/sites/docs' + MS.InformationManagement.Services.ReportService.get_path());
    MS.InformationManagement.CostReport.StartDateControl = $('input[id*="' + startDateID + '"]');
    MS.InformationManagement.CostReport.EndDateControl = $('input[id*="' + endDateID + '"]');
    $('#' + tabContainerID).tabs(
        {
            cookie: { expires: 30 }
            , select: function (event, ui) {
                if (ui.index === 1 && MS.InformationManagement.CostReport.Plotted === false) {
                    MS.InformationManagement.CostReport.Plotted = true;
                    MS.InformationManagement.CostReport.DrawPlot();
                }
            }
        }
    );

}

MS.InformationManagement.CostReport.DrawPlot = function () {
    var startDate = MS.InformationManagement.CostReport.StartDateControl.val();
    var endDate = MS.InformationManagement.CostReport.EndDateControl.val();
    $('#' + MS.InformationManagement.CostReport.TabContainerID).ajaxloader({ fullscreen: false, baseurl: '/sites/docs', autoshow: false });
    $('#' + MS.InformationManagement.CostReport.TabContainerID).ajaxloader('show');
    MS.InformationManagement.Services.ReportService.GetCostReport(
        startDate
        , endDate
        , MS.InformationManagement.CostReport.OnReportGenerated
        , MS.InformationManagement.CostReport.OnAjaxError
    );
}

MS.InformationManagement.CostReport.OnReportGenerated = function (result, userState) {
    $('#' + MS.InformationManagement.CostReport.TabContainerID).ajaxloader('hide');
    if (result.length > 0) {
        var data = [[]];
        for (var i = 0; i < result.length; i++) {
            data[0].push([result[i].Label, result[i].Value]);
        }
        var plot = $.jqplot(MS.InformationManagement.CostReport.PlottingElementID, data, {
            seriesDefaults: {
                renderer: $.jqplot.DonutRenderer,
                rendererOptions: {
                    sliceMargin: 3,
                    startAngle: -90,
                    showDataLabels: true
                }
            }
            , legend: { show: true, location: 'e' }
        });
    }
}

MS.InformationManagement.CostReport.OnAjaxError = function (exception, userState) {
    $('#' + MS.InformationManagement.CostReport.TabContainerID).ajaxloader('hide');
    MS.InformationManagement.Common.ShowStatus('Ha ocurrido una excepción mientras se procesaba su peticion: ' + exception.get_message(), 'red', 6000);
}